package com.atom.wyz.new_opengl.base;

import android.content.res.Resources;
import android.opengl.GLES30;
import android.util.Log;
import android.view.View;

import com.atom.wyz.new_opengl.util.MatrixState;
import com.atom.wyz.new_opengl.util.ShaderManager;
import com.atom.wyz.new_opengl.util.ShaderUtil;
import com.atom.wyz.new_opengl.util.SimpleShaderBuffer;
import com.atom.wyz.new_opengl.util.SimpleShaderHandle;

public class TextureRect {

    SimpleShaderHandle shaderHandle;
    SimpleShaderBuffer shaderBuffer;
    float width;
    float height;
    // 不为空 就是自己获取的
    Resources res ;


    public TextureRect() {
        shaderBuffer = new SimpleShaderBuffer();
    }

    public TextureRect(Resources res) {
        this() ;
        this.res = res ;
        initProgram(res);
    }

    public TextureRect( float UNIT_SIZE) {
        this(UNIT_SIZE, 1, 1);
    }
    public TextureRect( float UNIT_SIZE, float width, float height) {
        this();
        this.width = width;
        this.height = height;
        initVertexData(UNIT_SIZE);
    }

    public TextureRect(Resources res , final float UNIT_SIZE, float width, float height) {
        this(UNIT_SIZE , width , height);
        initProgram(res);
    }

    public void initVertexData(final float UNIT_SIZE) {

        float vertices[] = new float[]
                {
                        -width * UNIT_SIZE, height * UNIT_SIZE, 0,
                        -width * UNIT_SIZE, -height * UNIT_SIZE, 0,
                        width * UNIT_SIZE, height * UNIT_SIZE, 0,

                        -width * UNIT_SIZE, -height * UNIT_SIZE, 0,
                        width * UNIT_SIZE, -height * UNIT_SIZE, 0,
                        width * UNIT_SIZE, height * UNIT_SIZE, 0
                };

        shaderBuffer.setVertexBuffer(vertices);

        float textures[] = new float[]
                {
                        0, 1, 0, 0, 1, 1,
                        0, 0, 1, 0, 1, 1
                };
        float textures2[] = new float[]
                {
                        0, 0, 0, 1, 1, 0,
                        0, 1, 1, 1, 1, 0
                };
        shaderBuffer.setTexcoorBuffer(textures);
    }
    public SimpleShaderBuffer getShaderBuffer() {
        return shaderBuffer;
    }

    public void initProgram(Resources res ){
        this.res = res ;
        String mVertexShader = ShaderUtil.loadFromAssetsFile("shader/vertex.sh", res);
        String mFragmentShader = ShaderUtil.loadFromAssetsFile("shader/frag.sh", res);
        initShader(ShaderUtil.createProgram(mVertexShader, mFragmentShader));
    }
    public void initShader(int mProgram) {
        shaderHandle = new SimpleShaderHandle(mProgram);
    }

    public void drawSelf(int texId) {
        if(res == null){
            initShader(ShaderManager.getTextureShaderProgram());
        }
        if (shaderHandle == null || shaderBuffer == null) return;
        shaderHandle.program()
                .bindMVPMaxtrixHandle(MatrixState.getFinalMatrix())
                .bindMMaxtrixHandle(MatrixState.getMMatrix())
                .bindVPMaxtrixHandle(MatrixState.getVPMatrix())
                .bindCameraHandle(MatrixState.cameraLocationFB)
                .bindLightHandle(MatrixState.lightLocationFB)
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer())
                .bindNormalHandle(shaderBuffer.getNormalBuffer())
                .bindDefaultTextureHandle(texId)
                .run(GLES30.GL_TRIANGLES, shaderBuffer.vCount);
    }
}
